home *** CD-ROM | disk | FTP | other *** search
- /*
- * This software is copyright 1992 by Robert Morris.
- * You may freely redistribute this software as shareware
- * if you do so in the same form as you got it. If you find
- * this software useful, please send $12 to:
- * Robert Morris
- * P.O. Box 1044
- * Harvard Square Station
- * Cambridge, MA 02238
- * ecognome@aol.com
- * If you incorporate any of this software in any kind of
- * commercial product, please send $2 per copy distributed
- * to the above address.
- */
-
- /*
- * General-purpose package for streams of bits.
- * The high bit in a byte is "first" is bigendian is 1, otherwise last.
- */
-
- struct bits{
- unsigned char *bits;
- long nbits;
- long nextbit; /* next bit to read beyond the cached bits */
- long cache; /* cache >> 1 is first cached bit. up to 31 bits here. */
- long cached; /* how many bits cached in cache */
- char bigendian;
- };
-
- #define SeekBits(b, bitno) ((b)->nextbit = (bitno), (b)->cached = 0)
- #define TellBits(b) ((b)->nextbit - (b)->cached)
- #define TestBitsEOF(b) (TellBits(b) >= (b)->nbits)
- #define SetBitsLength(b, n) ((b)->nbits = (n), (b)->cached = 0)
-
- #define ReadBit(b) ((b)->cached > 0 ? \
- ((b)->cached -= 1, ((b)->cache >>= 1), ((b)->cache & 1)) : \
- _ReadBit(b))
-
- #define Peek4Bits(b) (((b)->cached < 4 ? FillBitCache(b) : 0), \
- ((b)->cache >> 1) & 0xf)
-
- #define Read2Bits(b, dst) (((b)->cached < 2 ? FillBitCache(b) : 0), \
- (dst) = (((b)->cache >> 1) & 3), \
- (b)->cached -= 2, \
- (b)->cache >>= 2)
-
- #define TossBits(b, n) ((b)->cached -= (n), (b)->cache >>= (n))
-
- #define WriteBitSpan(b, bit, n) { long bitno; long byteno; \
- bitno = (b)->nextbit & 7; \
- if((n) <= (8-bitno)){ \
- byteno = (b)->nextbit >> 3; \
- (b)->bits[byteno] = (((bit)?0xff:0) >> bitno) | \
- ((b)->bits[byteno] & (0xff00 >> bitno)); \
- (b)->nextbit += (n); \
- } else { \
- _WriteBitSpan((b), (bit), (n)); \
- } \
- }
-
- OSErr NewBits(struct bits *, unsigned char *data, long nbits, char bigendian, int writeable);
- long _ReadBit(struct bits *);
- OSErr WriteBits(struct bits *, long bits, long n);
- OSErr ScanBits(struct bits *b, long wanted);
- OSErr _WriteBitSpan(struct bits *b, long bit, long n);
- OSErr FillBitCache(struct bits *b);